home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Extra 1996 #3 / AmigaPlus_CD-ROM-EXTRA_Nr.3.bin / aminet-spiele / role on / larn / amiga.c < prev    next >
C/C++ Source or Header  |  1997-12-31  |  7KB  |  294 lines

  1. #ifdef AMIGA
  2.   
  3. #include <intuition/intuition.h>
  4.   
  5. /*
  6.  *
  7.  *    Author : Simon J Raybould.    (sie@fulcrum.bt.co.uk).
  8.  *
  9.  *    Date   : 25th January 1991.
  10.  *
  11.  *    Desc   : Amiga specific code for Larn
  12.  *
  13.  */
  14.   
  15.   
  16. struct NewWindow nw = {
  17.   0, 0, 640, 200, -1,-1, NULL, SMART_REFRESH|ACTIVATE|BORDERLESS, NULL,
  18.   NULL, NULL, NULL, NULL, 0,0, 0,0, WBENCHSCREEN
  19. };
  20.  
  21.  
  22. /* Opens/allocations we'll need to clean up */
  23. struct Library  *IntuitionBase = NULL;
  24. struct Window   *win = NULL, *OpenWindow();
  25. struct IOStdReq *writeReq = NULL;    /* I/O request block pointer */
  26. struct MsgPort  *writePort = NULL;   /* replyport for writes      */   
  27. struct IOStdReq *readReq = NULL;     /* I/O request block pointer */
  28. struct MsgPort  *readPort = NULL;    /* replyport for reads       */   
  29. struct MsgPort  *CreatePort();
  30. BOOL OpenedConsole = FALSE;
  31.  
  32. #define AM_ECHO    1
  33. #define AM_CBREAK  2
  34.  
  35. void CloseConsole(), ConPuts(), QueueRead(), AmEnd();
  36. BYTE OpenConsole();
  37. UBYTE ibuf, GetchFlags = AM_ECHO;
  38.  
  39. void AmInit()
  40. {
  41.   ULONG conreadsig, windowsig;
  42.   BYTE error;
  43.   
  44.   if(!(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0))) {
  45.     printf("Can't open intuition\n");
  46.     AmEnd();
  47.     exit(10);
  48.   }
  49.   
  50.   /* Create reply port and io block for writing to console */
  51.   if(!(writePort = CreatePort("LARN.console.write",0))) {
  52.     printf("Can't create write port\n");
  53.     AmEnd();
  54.     exit(10);
  55.   }
  56.   
  57.   if(!(writeReq = (struct IOStdReq *)
  58.        CreateExtIO(writePort,(LONG)sizeof(struct IOStdReq)))) {
  59.     printf("Can't create write request\n");
  60.     AmEnd();
  61.     exit(10);
  62.   }
  63.   
  64.   /* Create reply port and io block for reading from console */
  65.   if(!(readPort = CreatePort("LARN.console.read",0))) {
  66.     printf("Can't create read port\n");
  67.     AmEnd();
  68.     exit(10);
  69.   }
  70.   
  71.   if(!(readReq = (struct IOStdReq *)
  72.        CreateExtIO(readPort,(LONG)sizeof(struct IOStdReq)))) {
  73.     printf("Can't create read request\n");
  74.     AmEnd();
  75.     exit(10);
  76.   }
  77.   /* Open a window */
  78.   if(!(win = OpenWindow(&nw))) {
  79.     printf("Can't open window\n");
  80.     AmEnd();
  81.     exit(10);
  82.   }
  83.   
  84.   /* Now, attach a console to the window */
  85.   if(error = OpenConsole(writeReq,readReq,win)) {
  86.     printf("Can't open console.device\n");
  87.     AmEnd();
  88.     exit(10);
  89.   } else
  90.     OpenedConsole = TRUE;
  91.  
  92.   QueueRead(readReq,&ibuf); /* send the first console read request */
  93.   conreadsig = 1 << readPort->mp_SigBit;
  94.   windowsig = 1 << win->UserPort->mp_SigBit;
  95. }
  96.  
  97. void AmEnd()
  98. {
  99.   if(OpenedConsole) {
  100.     if(!(CheckIO(readReq))) AbortIO(readReq);
  101.     WaitIO(readReq);
  102.     CloseConsole(writeReq);
  103.   }
  104.   if(readReq)       DeleteExtIO(readReq);
  105.   if(readPort)      DeletePort(readPort);
  106.   if(writeReq)      DeleteExtIO(writeReq);
  107.   if(writePort)     DeletePort(writePort);
  108.   if(win)           CloseWindow(win);
  109.   if(IntuitionBase) CloseLibrary(IntuitionBase);
  110. }
  111.  
  112.  
  113. /* Attach console device to an open Intuition window.
  114.  * This function returns a value of 0 if the console 
  115.  * device opened correctly and a nonzero value (the error
  116.  * returned from OpenDevice) if there was an error.
  117.  */
  118. BYTE OpenConsole(writereq, readreq, window)
  119.      struct IOStdReq *writereq;
  120.      struct IOStdReq *readreq;
  121.      struct Window *window;
  122. {
  123.   BYTE error;
  124.   
  125.   writereq->io_Data = (APTR) window;
  126.   writereq->io_Length = sizeof(struct Window);
  127.   error = OpenDevice("console.device", 0, writereq, 0);
  128.   readreq->io_Device = writereq->io_Device; /* clone required parts */
  129.   readreq->io_Unit   = writereq->io_Unit;
  130.   return(error);   
  131. }
  132.  
  133. void CloseConsole(struct IOStdReq *writereq)
  134. {
  135.   CloseDevice(writereq);
  136. }
  137.  
  138. /* Output a single character to a specified console 
  139.  */ 
  140. void ConPutChar(struct IOStdReq *writereq, UBYTE character)
  141. {
  142.   writereq->io_Command = CMD_WRITE;
  143.   writereq->io_Data = (APTR)&character;
  144.   writereq->io_Length = 1;
  145.   DoIO(writereq);
  146.   /* command works because DoIO blocks until command is done
  147.    * (otherwise ptr to the character could become invalid)
  148.    */
  149. }
  150.  
  151.  
  152. /* Output a stream of known length to a console 
  153.  */ 
  154. void ConWrite(struct IOStdReq *writereq, UBYTE *string, LONG length)
  155. {
  156.   writereq->io_Command = CMD_WRITE;
  157.   writereq->io_Data = (APTR)string;
  158.   writereq->io_Length = length; 
  159.   DoIO(writereq);
  160.   /* command works because DoIO blocks until command is done
  161.    * (otherwise ptr to string could become invalid in the meantime)
  162.    */
  163. }
  164.  
  165.  
  166. /* Output a NULL-terminated string of characters to a console 
  167.  */ 
  168. void ConPuts(struct IOStdReq *writereq,UBYTE *string)
  169. {
  170.   writereq->io_Command = CMD_WRITE;
  171.   writereq->io_Data = (APTR)string;
  172.   writereq->io_Length = -1;  /* means print till terminating null */
  173.   DoIO(writereq);
  174. }
  175.  
  176. /* Queue up a read request to console, passing it pointer
  177.  * to a buffer into which it can read the character
  178.  */
  179. void QueueRead(struct IOStdReq *readreq, UBYTE *whereto)
  180. {
  181.   readreq->io_Command = CMD_READ;
  182.   readreq->io_Data = (APTR)whereto;
  183.   readreq->io_Length = 1;
  184.   SendIO(readreq);
  185. }
  186.  
  187. /* Check if a character has been received.
  188.  * If none, return -1 
  189.  */
  190. LONG ConMayGetChar(struct MsgPort *msgport, UBYTE *whereto)
  191. {
  192.   register temp;
  193.   struct IOStdReq *readreq;
  194.   
  195.   if (!(readreq = (struct IOStdReq *)GetMsg(msgport))) return(-1);
  196.   temp = *whereto;                /* get the character */
  197.   QueueRead(readreq,whereto);     /* then re-use the request block */
  198.   return(temp);
  199. }
  200.  
  201. /* Wait for a character
  202.  */
  203. UBYTE ConGetChar(struct MsgPort *msgport, UBYTE *whereto)
  204. {
  205.   register temp;
  206.   struct IOStdReq *readreq;
  207.   
  208.   WaitPort(msgport);
  209.   readreq = (struct IOStdReq *)GetMsg(msgport);
  210.   temp = *whereto;               /* get the character */
  211.   QueueRead(readreq,whereto);    /* then re-use the request block*/
  212.   return((UBYTE)temp);
  213. }
  214.  
  215. sleep(int n)
  216. {
  217.   Delay(50*n);
  218.   return 0;
  219. }
  220.  
  221. DoShell()
  222. {
  223.   printf("DoShell() - NOT IMPLEMENTED\n");
  224. }
  225.  
  226. void WriteConsole(char *Ptr, int Len)
  227. {
  228.   ConWrite(writeReq, Ptr, Len);
  229. }
  230.  
  231. char AmGetch()
  232. {
  233.   static char Buffer[256];
  234.   static int WPos = 0, RPos = 0;
  235.  
  236.   if(GetchFlags & AM_CBREAK) {
  237.     RPos = WPos = 0;
  238.     *Buffer = ConGetChar(readPort, &ibuf);
  239.     return *Buffer;
  240.   } else {
  241.     if(RPos>=WPos) {
  242.       /* Reset buffer markers */
  243.       RPos = WPos = 0;
  244.       /* Buffer up till CR spotted */
  245.       do
  246.     Buffer[WPos] = ConGetChar(readPort, &ibuf);
  247.       while(Buffer[WPos++] != '\r');
  248.     }
  249.     return Buffer[RPos++];
  250.   }
  251. }
  252.  
  253. AmCBreak(int Flag)
  254. {
  255.   if(Flag)
  256.     GetchFlags |= AM_CBREAK;
  257.   else
  258.     GetchFlags &= ~AM_CBREAK;
  259. }
  260.  
  261. AmEcho(int Flag)
  262. {
  263.   if(Flag)
  264.     GetchFlags |= AM_ECHO;
  265.   else
  266.     GetchFlags &= ~AM_ECHO;
  267. }
  268.  
  269. FlushKBD()
  270. {
  271.   /* TODO */
  272. }
  273.  
  274. /*
  275.  * Missing unix system calls.
  276.  */
  277.  
  278. fork()
  279. {
  280.   return -1;
  281. }
  282.  
  283. getpid()
  284. {
  285.   return FindTask(0L);
  286. }
  287.  
  288. kill()
  289. {
  290.   return 0;
  291. }
  292.  
  293. #endif /* AMIGA */
  294.